﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Dhtl.Service.IService;
using dai_hoc_tuong_lai.Areas.Admin.Models;
using DataBase.Daihoctuonglai;
using FarmeWork.Helper;
using Comom.Project.ControllerCom;

namespace dai_hoc_tuong_lai.Areas.Admin.Controllers
{
     [Authorize(Roles = "Admin,employee")]
    public class TopicAdminController : UTController
    {
        //
        // GET: /Admin/TopicAdmin/
        private readonly ITopic _iTopic;

        public TopicAdminController(ITopic iTopic)
        {
            this._iTopic = iTopic;
        }

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult List()
        {

            var listEntity = _iTopic.List();
            List<TopicModel> model = new List<TopicModel>();
            try
            {
                foreach (var item in listEntity)
                {
                    TopicModel ml = new TopicModel();
                    TopicModel.MapFrom(item, ref  ml);
                    model.Add(ml);
                }
            }
            catch { }

            return PartialView("_List", model);
        }

        public ActionResult Add()
        {
            TopicModel model = new TopicModel();
            model.Id = 0;
            return View("_Add", model);
        }

        public ActionResult Update(int id)
        {
            TopicModel model = new TopicModel();
            var entity = _iTopic.GetById(id);
            try
            {
                TopicModel.MapFrom(entity, ref model);
            }
            catch { }

            return View("_Update", model);
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult AddOrUpdate(TopicModel model)
        {

            try
            {
                if (string.IsNullOrEmpty(model.Title))
                {
                    return JsonError("Nhập tên topic vào");
                }

                if (string.IsNullOrEmpty(model.Noidung))
                {
                    return JsonError("Nhập nội dung topic vào");
                }

                if (string.IsNullOrEmpty(model.Code))
                {
                    return JsonError("nhập session code vào");
                }

                var entity = new Topic();
                TopicModel.MapFrom(model, ref entity);
                _iTopic.AddOrUpdate(entity);

                return JsonSuccess(Url.Action("Index"), null);
            }
            catch (Exception ex)
            {
                return JsonError(ex.ToString());
            }
        }

        public ActionResult Delete(int id)
        {
            try
            {
                _iTopic.delete(id);
                return JsonSuccess(Url.Action("Index"), null);
            }
            catch (Exception ex)
            {
                return JsonError(ex.ToString());
            }
        }

    }
}
